﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Insteon.Messages.Insteon
{

    internal class AllLinkingCompletedMessage : Message
    {
        internal const int MessCommand = 0x53;
        internal const int MessLength = 10;

        internal readonly byte LinkCode;
        internal readonly byte Group;
        internal readonly InsteonAddress InsteonAddress;
        internal readonly byte Category;
        internal readonly byte SubCategory;
        internal readonly byte FirmwareVersion;

        public AllLinkingCompletedMessage(byte[] buffer, UInt16 Length, UInt64 Number, out UInt16 Size)
            : base(buffer, Length, Number)
        {
            if (!Check(buffer, Length))
            {
                throw new Exception("Invalid message format");
            }
            LinkCode = (byte)new LinkCode(buffer[2]).Value;
            Group = buffer[3];
            InsteonAddress = new global::Insteon.InsteonAddress(buffer, 4);
            Category = buffer[7];
            SubCategory = buffer[8];
            FirmwareVersion = buffer[9];
            Size = MessLength;
        }

        internal static bool Check(byte[] buffer, UInt16 length)
        {
            if ((length >= MessLength) && (buffer[1] == MessCommand))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public override string ToString()
        {
            return PrintCommonMessage(ReceivedTime, Number, this, null, InsteonAddress, null, 0, 9, null);
        }

        internal override void RouteToDevice(InsteonInterface Interface)
        {
            Interface.RouteToTarget(this, InsteonAddress);
        }
    }
}
